package com.ytph.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;
/**
 * qrcode二维码生成
 * @author administrator
 *
 */
public class CreateQRCode {
	public static void main(String[] args) throws Exception {

		Qrcode x = new Qrcode();
		// 纠错等级
		x.setQrcodeErrorCorrect('M');
		// N 代表数字 A 代表a-Z B 代表其他字符
		x.setQrcodeEncodeMode('B');
		x.setQrcodeVersion(7);
		String qrData = "hava a good time!~"; // 内容

		int width = 67 + 12 * (7 - 1);
		int height = 67 + 12 * (7 - 1);
		// 创建图片缓冲区
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// java 画图工具
		Graphics2D gs = bufferedImage.createGraphics();// 创建画图工具
		gs.setBackground(Color.WHITE);
		gs.setColor(Color.BLACK);
		gs.clearRect(0, 0, width, height);

		// 偏移量
		int pixoff = 2;

		byte[] d = qrData.getBytes("gb2312"); // 如果是汉字就要加gb2312
		// byte[] d = qrData.getBytes();
		if (d.length > 0 && d.length < 120) {

			boolean[][] s = x.calQrcode(d);

			for (int i = 0; i < s.length; i++) {
				for (int j = 0; j < s.length; j++) {
					if (s[j][i]) {
						gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
					}
				}
			}
		}
		//关流
		gs.dispose();
		bufferedImage.flush();
		String path = "D:/code/qrcode.png";
		ImageIO.write(bufferedImage, "png", new File(path));
	}
}
